package com.learn.controller;


import cn.hutool.core.convert.impl.MapConverter;
import com.common.utils.Result;
import com.feign.clients.UserClient;
import com.feign.pojo.UserVo;
import com.learn.config.VideoConfig;
import com.learn.pojo.dto.CallbackDataDto;
import com.learn.server.VideoChangeServer;
import com.learn.utils.Mp4ExtractAudioUtils;
import com.learn.utils.TenXunYunUtil;
import com.tencentcloudapi.asr.v20190614.models.TaskStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/learn")
@Api(value = "视频转文字工具")
@Slf4j
public class VideoChangeController {

//    @Autowired
    private UserClient userClient;

    @Autowired
    private Mp4ExtractAudioUtils mp4ExtractAudioUtils;

    @Autowired
    private TenXunYunUtil tenXunYunUtil;

    @Autowired
    private VideoChangeServer videoChangeServer;

    @Autowired
    private VideoConfig videoConfig;

    @GetMapping
    public Result changeWrite(){
        Result result = userClient.selectUserDetail(1);
        UserVo data = (UserVo)result.getData();
        System.out.println(data);
        return Result.success();
    }

    /**
     * url 视频文件地址
     * 通过视频文件解析成音频文件拿到对象键进行下一步操作 *
     * * * */
    @GetMapping("/url")
    @ApiOperation("视频转文字接口")
    public Result upDate(@RequestParam String url){
        String s = mp4ExtractAudioUtils.extractAudio(url, videoConfig.getPcmUrl());
        if (s.isEmpty()){
            return Result.error("视频文件解析失败，练习群主");
        }
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        Long taskId = tenXunYunUtil.getClient(videoConfig.getAudioUrl() + s);
        if (taskId==null){
            return Result.error("发起音频转文字失败");
        }
        executorService.shutdown();
        HashMap<String, Long> map = new HashMap<>();
        map.put("taskId",taskId);
        return Result.success(map);
    }


    /**
     * 语音识别回调
     * * * */
    @ApiOperation("语音转文字回调接口")
    @PostMapping("/callback")
    public Result callback(@RequestBody CallbackDataDto callbackData){
        String text = callbackData.getData().getText();
        System.out.println(text);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("message","success");
        return Result.success(map);
    }

    /**
     * 获取视频转文字的结果
     * * * */
    @GetMapping("/text")
    @ApiOperation("获取文字的接口")
    public Result getText(Long taskId){
        String text=videoChangeServer.selectText(taskId);
        HashMap<String, String> map = new HashMap<>();
        if (text!=null){
            map.put("text",text);
            return Result.success(map);
        }
        TaskStatus taskStatus = tenXunYunUtil.getText(taskId);
        if (taskStatus==null) return Result.error("未知错误");
        switch (String.valueOf(taskStatus.getStatus())){
            case "3" :
                log.error(taskStatus.getErrorMsg());
               return Result.error("音频识别失败");
            case "2" :
                map.put("text",taskStatus.getResult());
                videoChangeServer.insertTask(taskStatus);
                return  Result.success(map);
            case "1":
                return Result.error("视频正在加速转文字中请等待一分钟");
            case "0":
                return Result.error("视频正在加速转文字中请等待五分钟");
            default:
                System.out.println("没有匹配上");
        }
       return Result.error("出现了不可预料的识别错误");
    }

}
